package com.xunlai.infra.permission.manager.web.controller;

import com.xunlai.infra.permission.common.constant.ExtendClaimsConstants;
import com.xunlai.infra.permission.common.exception.PermissionBusinessException;
import com.xunlai.infra.permission.manager.web.service.AdminUserService;
import com.xunlai.infra.permission.manager.web.vo.BasePageRespVo;
import com.xunlai.infra.permission.manager.web.vo.user.CreateUserAccountReqVo;
import com.xunlai.infra.permission.manager.web.vo.BaseResponse;
import com.xunlai.infra.permission.manager.web.vo.user.UserAccountVo;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal;
import org.springframework.web.bind.annotation.*;

/**
 * @author liang
 * @date 2022/12/2 15:36
 */
@RestController
@RequestMapping("system/user")
@Slf4j
public class AdminUserController {


    private AdminUserService adminUserService;

    @GetMapping("profile")
    @PreAuthorize("hasAnyRole('USER','ADMIN')")
    public BaseResponse<Object> login(@AuthenticationPrincipal OAuth2AuthenticatedPrincipal pri){
        String tenant = pri.getAttribute(ExtendClaimsConstants.TENANT);
        String id = pri.getAttribute(ExtendClaimsConstants.ID);
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return BaseResponse.success(pri,"用户信息");
    }


    @PostMapping("account/create")
    @PreAuthorize("hasAnyRole('ADMIN','SUPER_ADMIN')")
    public BaseResponse<Long> createUserAccount(@RequestBody @Valid CreateUserAccountReqVo createUserAccountReqVo){
        try {
            return BaseResponse.success(adminUserService.createUserAccount(createUserAccountReqVo),"");
        } catch (PermissionBusinessException e) {
            return BaseResponse.error(e.getCode(),null,e.getMsg());
        }
    }

    @PostMapping("info/create")
    @PreAuthorize("hasAnyRole('EMPLOYEE','ADMIN','SUPER_ADMIN')")
    public BaseResponse<Long> createUserInfo(@RequestBody @Valid CreateUserAccountReqVo createUserAccountReqVo){
        try {
            return BaseResponse.success(adminUserService.createUserAccount(createUserAccountReqVo),"");
        } catch (PermissionBusinessException e) {
            return BaseResponse.error(e.getCode(),null,e.getMsg());
        }
    }

    @GetMapping("account/list")
    @PreAuthorize("hasAnyRole('ADMIN','SUPER_ADMIN')")
    public BaseResponse<BasePageRespVo<UserAccountVo>> listUserAccount(
            @RequestParam(required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(required = false,defaultValue = "10") Integer pageSize){
        return BaseResponse.success(adminUserService.pageListUserAccount(pageNum,pageSize),"SUCCESS");
    }

//    public RestResponse<>

    @Autowired
    public void setAdminUserService(AdminUserService adminUserService) {
        this.adminUserService = adminUserService;
    }
}
